<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\validate\Ncjy as NcjyValidate;
use app\admin\model\Ncjy as NcjyModel;


class Ncjy extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1, $q = '')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];

        if($q){
            $pageParam['query']['q'] = $q;
            $list = NcjyModel::with('admin')->where('host_name|cook_name', 'LIKE', "%$q%")->order('ncjy_id desc')->paginate(15,false,$pageParam);
        }else{
            $list = NcjyModel::with('admin')->order('ncjy_id desc')->paginate(15);
        }
        //$list = NcjyModel::with('admin')->order('ncjy_id desc')->paginate(4);
        $ncjyCount = NcjyModel::count();
        $this->assign('ncjyCount', $ncjyCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }
    //所有人都能查家宴列表
    public  function all($page = 1, $q = '')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];

        if($q){
            $pageParam['query']['q'] = $q;
            $list = NcjyModel::with('admin')->where('host_name|cook_name', 'LIKE', "%$q%")->order('ncjy_id desc')->paginate(15,false,$pageParam);
        }else{
            $list = NcjyModel::with('admin')->order('ncjy_id desc')->paginate(15);
        }
        //$list = NcjyModel::with('admin')->order('ncjy_id desc')->paginate(4);
        $ncjyCount = NcjyModel::count();
        $this->assign('ncjyCount', $ncjyCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }
    //根据用户id(admin_id)获得家宴的列表
    public function self($page = 1, $q = '')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];

        $admin_id = session('id');

        if($q){
            $pageParam['query']['q'] = $q;
            $list = NcjyModel::with('admin')->where('admin_id', '=', $admin_id)->where('host_name|cook_name', 'LIKE', "%$q%")->order('ncjy_id desc')->paginate(15,false,$pageParam);
        }else{
            $list = NcjyModel::with('admin')->where('admin_id', '=', $admin_id)->order('ncjy_id desc')->paginate(15);
        }
        //$list = NcjyModel::with('admin')->order('ncjy_id desc')->paginate(4);
        $ncjyCount = NcjyModel::where('admin_id', '=', $admin_id)->count();
        $this->assign('ncjyCount', $ncjyCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    // 根据ncjy_id取详细家宴信息
    public function detail(Request $request)
    {
        //dump($request->param('ncjy_id'));die;
        $ncjy_id = $request->param('ncjy_id');

        $ncjy = NcjyModel::get($ncjy_id);
        if($ncjy){
            $this->assign('ncjy', $ncjy);
        }else{
            echo '非法参数!';die;
        }
        $this->assign('ncjy', $ncjy);
        return $this->fetch();
    }

    public function add(Request $request)
    {
        if($request->isPost()){
            //dump($request->param());die;
            $validate = new NcjyValidate;
            //验证
            if(!$validate->check($request->param())){
                $this->error($validate->getError());
            }
            //添加
            $ncjy = new NcjyModel;
            //allowField(true)过虑post数组中非数据表字段数据
            $ncjy->allowField(true)->save($request->param());
            if(session('role_id') == 1)
            {
                $this->redirect('admin/Ncjy/index');
            }else{
                $this->redirect('admin/Sign/info');
            }
        }
        //前台存admin_id
        $this->assign('id', session('id'));
        return $this->fetch();
    }


    public function edit(Request $request)
    {
        //dump($request->param('ncjy_id'));die;
        $ncjy_id = $request->param('ncjy_id');
        $page = $request->param('page');
        $q = $request->param('q');

        $ncjy = NcjyModel::get($ncjy_id);
        if($ncjy){
            $this->assign('ncjy', $ncjy);
        }else{
            echo '非法参数!';die;
        }

        if($this->request->isPost()){
            $validate = new NcjyValidate;
            //验证
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            //更新
            $new_ncjy = new NcjyModel;
            $new_ncjy->allowField(true)->save($request->param(), ['ncjy_id' => $ncjy_id]);
            //跳转
            return redirect('admin/Ncjy/index',['page' => $page, 'q' => $q]);
        }

        $this->assign('page', $page);
        return $this->fetch();
    }

    public function del($ncjy_id = 0)
    {
        $ncjy = NcjyModel::get($ncjy_id);
        if($ncjy)
        {
            $this->assign('ncjy',$ncjy);
        } else {
            echo '非法参数！';die;
        }

        if($this->request->isPost()){
            NcjyModel::destroy($ncjy_id);
            return redirect('admin/Ncjy/index');
        }
        return $this->fetch();
    }

    public function export(Request $request)
    {
        if(request()->isPost())
        {
            $begin_date = $request->param('begin_date');
            $end_date = $request->param('end_date');
            $tel = $request->param('tel');
            $where = null;
            if($begin_date){
                $where[] = ['ncjy_time', '>=', strtotime($begin_date)];
            }

            if($end_date){
                $where[] = ['ncjy_time', '<=', strtotime($end_date)];
            }

//            if($tel){
//                $where[] = ['tel', '=', $tel];
//            }

            //导出数据
            // excel表头
            $headArr[] = '举办者姓名';
            $headArr[] = '举办者电话';
            $headArr[] = '举办地';
            $headArr[] = '日期';
            $headArr[] = '人数';
            $headArr[] = '事由';
            $headArr[] = '厨师姓名';
            $headArr[] = '厨师电话';
            $headArr[] = '厨师住址';
            $headArr[] = '填写时间';
            $headArr[] = '填写人';
            // 数据
//            dump($where);
//            die();
            if($where != null){
                $list = NcjyModel::with('admin')->where($where)->select();
            }else{
                $list = NcjyModel::with('admin')->select();
            }

            $data = array();
            foreach($list as $ncjy){
                // 获取学生选课数据
                $datatemp[0]=$ncjy->host_name;
                $datatemp[1]="'" . $ncjy->host_tel;
                $datatemp[2]=$ncjy->host_addr;
                $datatemp[3]=$ncjy->host_date;
                $datatemp[4]=$ncjy->num;
                $datatemp[5]=$ncjy->cause;
                $datatemp[6]=$ncjy->cook_name;
                $datatemp[7]="'" . $ncjy->cook_tel;
                $datatemp[8]=$ncjy->cook_addr;
                $datatemp[9]=$ncjy->ncjy_time;
                $datatemp[10]=isset($ncjy->admin->admin_name) ? $ncjy->admin->admin_name : '';
                //$datatemp[10]=$ncjy->admin->admin_name ?? '';

                if($tel){
                    if($tel == $ncjy->admin->tel){
                        $data[] = $datatemp;
                    }
                }else{
                    $data[] = $datatemp;
                }

                //$data[] = $datatemp;

            }
            //dump($data);die;
            $filename = '农村家宴信息表';
            if($data){
                export_excel($filename, $headArr, $data); // $filename excel名称 $headArr excel表头
            }
        }
        return $this->fetch();
    }


}
